<?php
namespace app\admin\model;

use think\Model;
use think\facade\Config;
class Material extends Model
{   
	/*查询数据 */
	public function get_data(){
		$req=request()->param();
        if(!empty($req['material'])||!empty($req['isindex'])){
            $list=Material::where([['status','=',1],['material','like',"%".$req['material']."%"]])->order('create_time','desc')
            ->paginate(Config::get('app.admin_page_number'),false,['query' => $req]);
        }else{
            $list=Material::where([['status','=',1]])->order('create_time','desc')
              ->paginate(Config::get('app.admin_page_number'),false,['query' => $req]);
        }
        $data['list']=$list;
        if(empty($req['material'])){$data['material']='';}else{$data['material']=$req['material'];}
        return $data;
	}
	/*获取一条数据用于修改*/
	public function get_edit($id){
        $data=Material::where([['status','=',1],['id','=',$id]])->find();
        return $data;  
	}
	/*新增*/
	public function insert_data($material_name,$des){
        $Material = new Material;
		$result=$Material->save([
		    'material'  =>  trim($material_name),
		    'des'  =>  trim($des),
		    'create_time' => date("Y-m-d H:i:s",time())
		]);
		return $result;
	}
	/*修改*/
	public function update_data($id,$material_name,$des){
        $data = Material::get($id);
        $data->material = $material_name;
        $data->des = $des;
        $result=$data->save();
		return $result;    
	}
	/*删除假动作实际为修改操作*/
	public function delete_data($id){
        $data = Material::get($id);
        if($data['status']!=1){
        	$result=['status'=>'0','msg'=>'无需操作'];
	        return json($result);
        }
		$data->status = '2';
		$result=$data->save();
		return $result;
	}
}
 